<?php

/* 这是关于ajax数据调用的地方 by liangcaijian
1、调用店铺展示里的下一页的商品
*/
class Lcj_ajaxApp extends MallbaseApp
{
	/* 执行整理网站数据任务 */
    function cleanuptask()
	{
		include_once(ROOT_PATH . '/includes/tasks/cleanup.task.php');
		$uptask = new CleanupTask;		
		$uptask->run();		
	}	
	//更新N天过期的厨窗推荐商品
	function updatewindow()
	{
		$goods_mod =& m('goods');
		$last_time = Conf::get('window_recom_day') ? Conf::get('window_recom_day') : 15;
		$day_7 = gmtime() - $last_time * 24 * 3600;//后退7天
		//$goods_mod->edit("window_recom > 0 and window_recom < {$day_7}", "window_recom = window_recom+{day_7}" );
		//$sql = "SELECT goods_id,window_recom FROM {$goods_mod->table} g WHERE if_show = 1 and window_recom > 0 and window_recom < " . $day_7;
		$sql = "SELECT goods_id,window_recom FROM {$goods_mod->table} g WHERE window_recom > 0";
		$goods_list = $goods_mod->getAll($sql);	
		print_r($goods_list);
		foreach ( $goods_list as $key => $goodslist)
		{
		}
		//$db=&db();
		//$db->query("update ecm_goods set window_recom = window_recom+". $day_7 ."WHERE if_show = 1 and window_recom > 0 and window_recom < " . $day_7);
	}
	
	//ajax我的购物车列表信息
	function ajax_cart()
	{
		$cart_model =& m('cart');
		$where_user_id = $this->visitor->get('user_id') ? " AND cart.user_id=" . $this->visitor->get('user_id') : '';
		$cart_items = $cart_model->find(array(
			'conditions'    => 'session_id = \'' . SESS_ID . "'" . $where_user_id,
			'fields'        => 'this.*',
		));
		if (!$cart_items)
		{
			$this->json_result(array( 'cart_list' =>  0 ));	
			exit;
		}
		$cart_subtotal = $cart_quantity = 0;
		$get_list_html .= '<div class="cart_shoping_wrap">';
		$get_list_html .= '<ul class="cs_list">';
		foreach ($cart_items as $keys => $cartitem)
		{
		  $get_list_html .= '<li class="cs_list_item"><dl>';
		  $get_list_html .= '<dt class="cs_list_img"><a href="goods-'.$cartitem['goods_id'].'.html" target="_blank"><img width=48 height=60 src="'.$cartitem['goods_image'].'"></a></dt>';
		  $get_list_html .= '<dd class="cs_list_title">';
		  $get_list_html .= '<p class="cs_list_name"><a href="goods-'.$cartitem['goods_id'].'.html" target="_blank">'.$cartitem['goods_name'].'</a></p>';
		  $get_list_html .= '<p>'.$cartitem['specification'].'</p>';
		  $get_list_html .= '<dd class="cs_list_price"><span>¥'.$cartitem['price'].'</span>x<em>'.$cartitem['quantity'].'</em></dd>';
		  $get_list_html .= '</dl></li>';
		  $cart_quantity += $cartitem['quantity'];
		  $cart_subtotal += $cartitem['price'] * $cartitem['quantity'];
		}
		$get_list_html .= '</ul><div class="cs_checkout">';
		$get_list_html .= '<p><span class="cs_checkout_num">共<em>'.$cart_quantity.'</em>件商品</span><span class="cs_checkout_price">金额总计：¥<em>'.$cart_subtotal.'</em></span></p>';
		$get_list_html .= '<p><a href="index.php?app=cart" class="cs_checkout_btn">去购物车结算</a></p>';
		$get_list_html .= '</div></div>';
		
		$this->json_result(array( 'cart_list' =>  $get_list_html, 'cart_quantity' =>  $cart_quantity ));		
	}
	
    /************************* js判断,用于header.html的调用,通用于静态页面 *************************/
	function js_user_login()
	{
		/***************** 判断会员是否已经登录,用于header.html的调用,通用于静态页面 ********************/
		if ($this->visitor->get('user_id'))
		{
			$get_user_html[1] = "欢迎您，<a href='".SITE_URL."/index.php?app=member'>".$this->visitor->get('user_name')."</a>" . 
			                 "&nbsp;[<a href='".SITE_URL."/index.php?app=member&act=logout'>".Lang::get('logout')."</a>]<em>|</em>" . 
							 "<a href='".SITE_URL."/index.php?app=message&act=newpm'>".Lang::get('pm');
			if ( $this->_get_new_message() )
			{
				$get_user_html[1] .= "(<font style='color:red'>" .$this->_get_new_message(). "</font>)";
			}
			$get_user_html[1] .= "</a>";			
		}
		else
		{
			$get_user_html[1] = "您好，欢迎来到近水楼台！请 <a href='".SITE_URL."/index.php?app=member&act=login'>登录</a><em>|</em>".
			                 "<a href='".SITE_URL."/index.php?app=member&act=register'>免费注册</a>";
		}
		
		/***************** js调用显示我的帐户下拉类别,用于header.html的调用,通用于静态页面 *****************/
		if ($this->visitor->get('store_id'))
		{
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=seller_order'>已卖出商品</a></li>";
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=my_goods&character=show'>商品管理</a></li>";
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=my_qa'>买家咨询</a></li>";
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=my_favorite'>我的收藏夹</a></li>";
		}else{
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=buyer_order'>我的订单</a></li>";
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=my_money&act=remit_alipay'>我的资金</a></li>";
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=my_favorite'>收藏的商品</a></li>";
            $get_user_html[2] .= "<li><a href='".SITE_URL."/index.php?app=my_favorite&type=stores'>收藏的店铺</a></li>";
		}
		
		/***************** js调用显示购物车数量,用于header.html的调用,通用于静态页面 *******************/
		$cart =& m('cart');
		$get_user_html[3] = $cart->get_kinds(SESS_ID, $this->visitor->get('user_id'));
					
		/* 返回结果 */
		$this->json_result(array(
           'userlogin' =>  $get_user_html[1],
		   'topmembe' =>  $get_user_html[2],
		   'cartkinds' =>  $get_user_html[3],
        ));
	}

	
	/* 调用店铺展示里的下一页的商品 */
    function getGoodsByStore()
    {
        /* 传入store_id，判断是否删除整个店铺的商品------ */
        $store_id = isset($_GET['store_id']) ? intval($_GET['store_id']) : 0;
		$now_page = isset($_GET['page']) ? intval($_GET['page']) : 0;
		$get_goods_html = '';
		if ($store_id <= 0 && $now_page <= 0)
        {
			return;
        }
		$goods_mod  =& m('goods');
		//--------------------------------分页-----------------------------------
		$goods_mod =& bm('goods', array('_store_id' => $store_id));
		$page = $this->_get_page(4);//每页商品个数
        $goods_list = $goods_mod->get_list(array(
			'conditions' => 'closed = 0 AND if_show = 1 AND default_spec > 0',
			'fields'     => 'goods_name, default_image,fcb_deal,fcb_region,fcb_credit,price',
			'count'      => true,//允许统计,必须的，否则显示不出分页
			'order'      => 'recommended desc,add_time desc',
            'limit'      => $page['limit'],
        ));	
        $page['item_count'] = $goods_mod->getCount();
        $this->_format_page($page);
		//---------------------------------分页-------------------------------------
	 if ($goods_list)
	 {
		if ( $now_page == 1 )
		{
			$get_goods_html = '<div class="leftimg"></div>';
		}else{
			$get_goods_html = '<div class="leftimg"><a href="javascript:next_show('.$store_id.','.$now_page.',1);"></a></div>';
		}
		$get_goods_html .= '<ul class="store_list_pic">';
		foreach ($goods_list as $goods_lists)
        {
			$get_goods_html .= '<li>';
			$get_goods_html .= '<p><a href="'.$site_url.'goods-'.$goods_lists['goods_id'].'.html" target="_blank">';
			if ( $this->_get_fangchaoban($goods_lists) )//防抄版处理
		    {
			   $get_goods_html .= '<img src="/data/system/goods_locked_bg.jpg" alt="'.$goods_lists['goods_name'].'"/></a></p>';
			}else{
				$get_goods_html .= '<img src="'.$goods_lists['default_image'].'" alt="'.$goods_lists['goods_name'].'"/></a></p>';
			}
			
			$get_goods_html .= '<h3><a href="'.$site_url.'goods-'.$goods_lists['goods_id'].'.html" target="_blank" title="'.$goods_lists['goods_name'].'">'.$goods_lists['goods_name'].'</a></h3>';
			$get_goods_html .= "<div class=price><span class=priceleft>".LANG::get('price_pf').":<em>".price_format($goods_lists['price'])."</em></span>";
			
			$get_goods_html .= "<a class='add_cache_zone' href='javascript:void(0)' onclick=add_cache_zone('".$goods_lists['goods_id']."','".$goods_lists['price']."','".$goods_lists['default_image']."',event);></a></div>";

			$get_goods_html .= '</li>';
		}
		$get_goods_html .= '</ul><div class="rightimg"><a href="javascript:next_show('.$store_id.','.$now_page.',0);"></a></div>';	
	 }
		/* 返回结果 */
		if ($get_goods_html){ echo($get_goods_html); }else{ return; }
    }
    /**
     *    检查VIP激活码是否为有效
     *
     *    @author    Garbin
     *    @return    void
     */
    function check_vip_code()
    {
        $vip_code = empty($_GET['vip_code']) ? null : htmldecode($_GET['vip_code']);
        if (!$vip_code)
        {
            echo ecm_json_encode(false);
            return;
        }
		//判断是否是有效的VIP卡
		$z_vip_mod =& m('z_vip');
		$sql = "SELECT count(*) FROM {$z_vip_mod->table} WHERE vip_sn = '{$vip_code}' AND use_user_id = 0 AND if_issue <= 0";
		$check_vip = $z_vip_mod->getOne($sql);
		if ($check_vip)
		{
			echo ecm_json_encode(true);
		}else{
			echo ecm_json_encode(false);
		}
		return;
    }	
	/* 后台/--快速登录用户或者会员都行吧 */
    function kx_login()
    {

		if ($_GET['lcj'] != 'yanzhengtongguo')
		{
            $this->show_warning('错误的访问');
            return;			
		}
		
		if ( $this->visitor->get('user_id') && $this->visitor->get('user_id') == intval($_GET['user_id']))
		{
		  header("location:".$_GET['ret_url']);
		  exit;			
		}

		if ( intval($_GET['user_id']) > 0 and htmldecode($_GET['user_name']) and htmldecode($_GET['password']))
		{
			/* 判断用户和密码,安全方式的判断它的登录路径 */
			$mod_user =& m('member');
			$user_info = $mod_user->get(array(
				'conditions'    => "user_id = '{$_GET['user_id']}' and user_name = '{$_GET['user_name']}' and password = '{$_GET['password']}' ",
				'fields'        => 'user_id',
			));
			/* 通过验证，执行登陆操作 */
			if ($user_info)
			{
				$this->_do_login($user_info['user_id']);
				if ($_GET['ret_url'])
				{
				   header("location:".$_GET['ret_url']);
				   exit;
				}else{
				   header("location:index.php?app=member");
				   exit;
				}	
			}else{
				$this->show_warning('错误的访问');
				return;		
			}
		}
		

	}
	
	/* json查询会员信息 ：订单数量，订单状态等*/
	function queryUserInfo()
	{
        $order_mod =& m('order');
		$user_id = $this->visitor->get('user_id');
		if ($user_id)
		{
		  $seller_stat = array();$buyer_stat = array();
		  /* 卖家提醒：待处理订单和待发货订单 */
		  if ($this->visitor->get('manage_store'))
		  {
			$goods_mod =& m('goods');
			$seller_stat = array(
			  'all_order' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}'"),
			  'pending'   => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND status = '".ORDER_PENDING."'"),
			  'accepted'  => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND status = '".ORDER_ACCEPTED."'"),
			  'shipped'  => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND status = '".ORDER_SHIPPED."'"),
			  'evaluation'  => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND status = '".ORDER_FINISHED."' AND evaluation_buyer_time=0"),
			  'finished'  => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND status = '".ORDER_FINISHED."'"),
			  'returns' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND returns > 0 AND returns < 40 "),
			  'returns_new' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE seller_id = '{$user_id}' AND returns = 1 AND returns_time > 0 "),
			  'goods_all' => $goods_mod->getOne("SELECT COUNT(*) FROM {$goods_mod->table} WHERE store_id = '{$user_id}'"),
			  'goods_show' => $goods_mod->getOne("SELECT COUNT(*) FROM {$goods_mod->table} WHERE store_id = '{$user_id}' AND if_show = '1'"),
			  'goods_hide' => $goods_mod->getOne("SELECT COUNT(*) FROM {$goods_mod->table} WHERE store_id = '{$user_id}' AND if_show = '0'"),
			);
			$this->json_result(array(
				'seller_allorder'  =>  $seller_stat['all_order'],
				'seller_pending'   =>  $seller_stat['pending'],
				'seller_accepted'  =>  $seller_stat['accepted'],
				'seller_shipped'   =>  $seller_stat['shipped'],
				'seller_finished'  =>  $seller_stat['finished'],
				'seller_canceled'  =>  $seller_stat['canceled'],
				'seller_returns'   =>  $seller_stat['returns'],
				'seller_returns_new' =>  $seller_stat['returns_new'],
				'seller_goods_all'   =>  $seller_stat['goods_all'],
				'seller_goods_show'  =>  $seller_stat['goods_show'],
				'seller_goods_hide'  =>  $seller_stat['goods_hide'],
			  ),'ok');
		  }else{
		  /* 买家提醒：待付款、待确认、待评价订单数 */
			$buyer_stat = array(
			'all_order' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}'"),
			'pending'  => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}' AND status = '".ORDER_PENDING."'"),
			'accepted' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}' AND status = '".ORDER_ACCEPTED."'"),
			'shipped'  => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}' AND status = '".ORDER_SHIPPED."'"),
			'evaluation' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}' AND status = '".ORDER_FINISHED."' AND evaluation_status = 0"),
			'finished' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}' AND status = '".ORDER_FINISHED."'"),
			'returns' => $order_mod->getOne("SELECT COUNT(*) FROM {$order_mod->table} WHERE buyer_id = '{$user_id}' AND returns > 0 AND returns < 40 "),
			 );		
			$this->json_result(array(
				'buyer_allorder'   =>  $buyer_stat['all_order'], 
				'buyer_pending'    =>  $buyer_stat['pending'],
				'buyer_accepted'   =>  $buyer_stat['accepted'],
				'buyer_shipped'    =>  $buyer_stat['shipped'],
				'buyer_evaluation' =>  $buyer_stat['evaluation'],
				'buyer_finished'   =>  $buyer_stat['finished'],
				'buyer_canceled'   =>  $buyer_stat['canceled'],
				'buyer_returns'    =>  $buyer_stat['returns'],
			  ),'ok');			 
		  }
		}	
	}
}
?>
